package cn.dnui_wwt524.dnuifood_16110100524.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_wwt524.dnuifood_16110100524.R;
import cn.dnui_wwt524.dnuifood_16110100524.bean.UserBean;
import cn.dnui_wwt524.dnuifood_16110100524.listener.OnRetrofitListener;
import cn.dnui_wwt524.dnuifood_16110100524.model.UserModel;

public class LoginActivity extends BaseActivity
        implements OnRetrofitListener<UserBean>{
    private EditText username;
    private EditText password;
    private Button btn_login;
    private Button btn_reg;
    String user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
    }

    @Override
    void initViews() {
        layout_file=R.layout.activity_login;
        setLayout(layout_file);
        username = findViewById(R.id.login_username);
        password = findViewById(R.id.login_password);
        btn_login= findViewById(R.id.login_denglu);
        btn_reg = findViewById(R.id.login_zhuce);

    }

    @Override
    void initEvents() {
        //UserModel model = new UserModel();
        //model.login("","",this);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.getText().toString().trim().equals("")||password.getText().toString().trim().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else
                    initDate();
            }

        });
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,ResultActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    void initDate() {
        UserModel userModel = new UserModel();
        userModel.login(username.getText().toString().trim(),password.getText().toString().trim(),this);

    }


    @Override
    public void onSuccess(UserBean object,int flag) {
        if(object.getUserid().equals("0")){
            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
        }
        else{
            saveUser(object.getUserid());
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity. this,"登录成功",Toast.LENGTH_SHORT).show();
    }}

    @Override
    public void onFailure(String msg) {
        Toast.makeText(LoginActivity.this,msg ,Toast.LENGTH_SHORT).show();


    }
    public void saveUser(String user_id){

        SharedPreferences sharedPreferences = getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username",username.getText().toString());
//        int userid = Integer.parseInt(user_id);
//        editor.putInt("userid",userid);
        editor.putString("userid",user_id);
        editor.putString("userpass",password.getText().toString());
        editor.commit();
    }


}